<?php

use_helper("I18N", "Javascript");


function swf_upload_javascript($upload_url, $post_params = array(), $options = array()) {
  
  $request = sfContext::getInstance()->getResponse();
	
  $request->addJavascript(sfConfig::get('sf_swfupload_js_dir').'swfupload.js');
  $request->addJavascript(sfConfig::get('sf_swfupload_js_dir').'swfupload.swfobject.js');
  $request->addJavascript(sfConfig::get('sf_swfupload_js_dir').'swfupload.custom_functions.js');
  $request->addJavascript(sfConfig::get('sf_swfupload_js_dir').'swfupload.queue.js');
  $request->addJavascript(sfConfig::get('sf_swfupload_js_dir').'fileprogress.js');
  $request->addJavascript(sfConfig::get('sf_swfupload_js_dir').'handlers.js');

  $request->addJavascript(sfConfig::get('sf_prototype_web_dir').'/js/prototype');
  
  $request->addStylesheet(sfConfig::get('sf_swfupload_css_dir').'default.css');
  
  $output = "";

  $post_params_out = _swf_upload_options_for_javascript($post_params);
  
  if (!isset($options["file_size_limit"])) {
  	$options["file_size_limit"] = sfConfig::get("app_swfupload_file_size_limit");
  } 
   
  if (!isset($options["file_types"])) {
  	$options["file_types"] = sfConfig::get("app_swfupload_file_types");
  } 

  if (!isset($options["file_types_description"])) {
  	$options["file_types_description"] = sfConfig::get("app_swfupload_file_types_description");
  } 

  if (!isset($options["file_upload_limit"])) {
  	$options["file_upload_limit"] = sfConfig::get("app_swfupload_file_upload_limit");
  }

  if (!isset($options["file_queue_limit"])) {
  	$options["file_queue_limit"] = sfConfig::get("app_swfupload_file_queue_limit");
  } 

  if (!isset($options["debug"])) {
  	$options["debug"] = sfConfig::get("app_swfupload_debug");
  } 
  
  $output .= "
    var swfu;

	SWFUpload.onload = function () {
		var settings = {
			flash_url : '".sfConfig::get('sf_swfupload_swf_dir')."swfupload_f9.swf',
			upload_url : '".url_for($upload_url,true)."',
			post_params: ".$post_params_out.",
			file_size_limit : '".$options["file_size_limit"]."',
			file_types : '".$options["file_types"]."',
			file_types_description : '".$options["file_types_description"]."',
			file_upload_limit : ".$options["file_upload_limit"].",
			file_queue_limit : ".$options["file_queue_limit"].",
			custom_settings : {
				progressTarget : 'fsUploadProgress',
				cancelButtonId : 'btnCancel'
			},
			debug: ".$options["debug"].",
	
			swfupload_loaded_handler : swfUploadLoaded,
			file_queued_handler : fileQueued,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			queue_complete_handler : queueComplete,

			minimum_flash_version : '9.0.28',
			swfupload_pre_load_handler : swfUploadPreLoad,
			swfupload_load_failed_handler : swfUploadLoadFailed
		};
	
		swfu = new SWFUpload(settings);
	
	}

  ";
  
  
  
  return javascript_tag($output);
}


function swf_upload_standard_html() {


$output = '
<div id="content">
	<h1> Multi Upload </h1>
	<h4> </h4>
	<form id="form1" action="# method="post" enctype="multipart/form-data">
		
		<div id="divSWFUploadUI" style="visibility: hidden;">
			<fieldset class="flash" id="fsUploadProgress">
			<legend>Image Queue</legend>
			</fieldset>
			<p id="divStatus">0 Files uploaded</p>

			<p>
				<input id="btnBrowse" type="button" value="Choose Files... (Max 100 MB)" style="font-size: 8pt;" />
				<input id="btnSubmit" type="button" value="Upload!" style="font-size: 8pt;" onClick="submitUpload()"/>
				<input id="btnCancel" type="button" value="Cancel" disabled="disabled" style="font-size: 8pt;" />
				<br />
			</p>
		</div>
		<noscript style="display: block; background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px;">
			We are sorry.  SWFUpload could not load.  You must have JavaScript enabled to enjoy SWFUpload.
		</noscript>
		<div id="divLoadingContent" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">

			SWFUpload is loading. Please wait a moment...
		</div>
		<div id="divLongLoading" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
			SWFUpload is taking a long time to load or the load has failed.  Please make sure JavaScript is enabled and that a working version of the Adobe Flash Player is installed.
		</div>
		<div id="divAlternateContent" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
			We are sorry.  SWFUpload could not load.  You may need to install or upgrade Flash Player.
			Visit the <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Adobe website</a> to get the Flash Player.
		</div>
	</form>

</div>


';
return  $output;
}

function _swf_upload_options_for_javascript($options)
  {
    $opts = array();
    foreach ($options as $key => $value)
    {
      $opts[] = "'$key':'$value'";
    }
    sort($opts);

    return "{".join(", ", $opts)."}";
}
